package br.com.canvasForAll.ws.resource;

import java.io.InputStream;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.io.IOUtils;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;

import br.com.canvasForAll.model.entity.Arquivo;
import br.com.canvasForAll.model.entity.Postit;
import br.com.canvasForAll.model.services.interfaces.ArquivoService;
import br.com.canvasForAll.model.services.interfaces.PerfilService;
import br.com.canvasForAll.model.services.interfaces.PostitService;

@Path("arquivo")
public class ArquivoWS {
	
	private final String UPLOADED_FILE_PATH = System.getProperty("user.home") + 
			"/workspace/canvasForAll-ics_WEB/WebContent/resources/files/";
	
	@EJB private PostitService postitService;
	@EJB private ArquivoService arquivoService;
	@EJB private PerfilService perfilService;
	
	@GET
	@Path("/arquivosPostit/{id_postit}")
	@Produces(MediaType.APPLICATION_JSON)
	public Collection<Arquivo> getArquivoPostit(@PathParam("id_postit") String idPostit) throws Exception{
		try{
			Long id = new Long(idPostit);
			Postit postit = postitService.obterPorId(id);
			return arquivoService.obterArquivoPostit(postit);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new WebApplicationException(Status.NOT_FOUND);
		}
	}
	
	@POST
	@Path("/upload")
	@Consumes(MediaType.MULTIPART_FORM_DATA)
	public Response uploadFile(MultipartFormDataInput input){
		
		try{
			String fileName = recuperaInputParts(input);
			Arquivo a = new Arquivo();
			a.setDataCriacao(new Date());
			a.setDataUltimaAtualizacao(new Date());
			a.setId(2L);
			a.setPerfil(perfilService.obterId(1L));
			a.setPostit(postitService.obterPorId(1L));
			a.setTituloArquivo("ARQUIVO TESTE");
			a.setUrl(fileName);
			
			arquivoService.salvarArquivo(a);
			
			
			return Response.status(200).entity("Upload de arquivo realizado com sucesso: " + fileName).build();
		
		} catch (Exception e) {
			e.printStackTrace();
			throw new WebApplicationException(Status.NOT_FOUND);
		}
	}
	
	private String recuperaInputParts(MultipartFormDataInput input) {
		String fileName = new String();
		Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
		List<InputPart> inputParts = uploadForm.get("file");
		
		for(InputPart ip : inputParts){
			try{
				MultivaluedMap<String, String> header = ip.getHeaders();
				fileName = arquivoService.getFileName(header);
				InputStream inputStream = ip.getBody(InputStream.class, null);
				
				byte[] bytes = IOUtils.toByteArray(inputStream);
				
				//fileName = UPLOADED_FILE_PATH + fileName;
				arquivoService.writeFile(bytes, UPLOADED_FILE_PATH + fileName);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return fileName;
	}
	
}
